import VueRouter from "vue-router";

//保存下来push方法 将来我们还会用到原push方法进行路由跳转
let originPush = VueRouter.prototype.push;

//接下来我们重写push|replace方法  
//第一个参数location参数像原push方法声明跳转的地方 resolve和reject传递成功与失败

VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        //如果成功 调用原来的push方法  
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => { }, () => { });
    }
}

const router = new VueRouter({
    // mode:history,
    routes: [
        {
            path: '/login',
            name: 'loginPage',
            component: () => import('../pages/loginView'),
        },
        {
            path: '/',
            name: 'managePage',
            component: () => import('../pages/managePage.vue'),
            redirect: '/home',
            meta:{isAuth:true},
            children: [
                {
                    path: '/home',
                    name: 'home',
                    component: () => import('../pages/homeView'),
                    meta:{isAuth:true,title:'主页'},
                },
                {
                    path: '/userManage',
                    name: 'userManage',
                    component: () => import('../pages/userManageView'),
                    meta:{isAuth:true,title:'用户管理'},
                },
                {
                    path: '/password',
                    name: 'password',
                    component: () => import('../pages/passwordView'),
                    meta:{isAuth:true,title:'密码管理'},
                },
                {
                    path: '/teacherManage',
                    name: 'teacherManage',
                    component: () => import('../pages/mangementView'),
                    redirect: '/teacherManage/main',
                    meta:{isAuth:true},
                    children: [
                        {
                            path: 'main',
                            name: 'mangementMain',
                            component: () => import('../pages/mangementView/mainView'),
                            meta:{isAuth:true,title:'教师管理'},
                        },
                        {
                            path: 'scArch',
                            name: 'mangementScArch',
                            component: () => import('../pages/mangementView/scArchView'),
                            meta:{isAuth:true,title:'科研成果管理'},
                        },
                        {
                            path: 'teachArch',
                            name: 'mangementTeachArch',
                            component: () => import('../pages/mangementView/teachArchView'),
                            meta:{isAuth:true,title:'教学成果管理'},
                        }
                    ]
                },
            ]
        },
        {
            path:'/',
            name:'teacherPage',
            component:()=>import('../pages/teacherPage.vue'),
            redirect: '/home',
            meta:{isAuth:true},
            children:[
                {
                    path:'/home',
                    name:'home',
                    component:()=>import('../pages/homeView'),
                    meta:{
                        isAuth:true,
                        user: 'tearcher',
                        title:'主页'
                    },
                },
                {
                    path:'/password',
                    name:'password',
                    component:()=>import('../pages/passwordView'),
                    meta:{isAuth:true,title:'密码管理'},
                },
                {
                    path:'/detial',
                    name:'detial',
                    component:()=>import('../pages/teacherView'),
                    redirect:'/detial/scArch',
                    meta:{isAuth:true},
                    children: [
                        {
                            path: 'scArch',
                            name: 'scArch',
                            component: () => import('../pages/teacherView/scArchView'),
                            meta:{isAuth:true,title:'科研成果管理'},
                        },
                        {
                            path: 'teachArch',
                            name: 'teachArch',
                            component: () => import('../pages/teacherView/teachArchView'),
                            meta:{isAuth:true,title:'教学成果管理'},
                        }
                    ]
                },
            ]
        }, 
    ]
})

 // eslint-disable-next-line
router.beforeEach((to,from,next)=>{
    if(to.meta.isAuth){
        if(localStorage.getItem('user')){
            next()
        }
        else{
            router.push({
                name:'loginPage'
            })
        }
    }
    else{
        next()
    }
})

 // eslint-disable-next-line
router.afterEach((to,from)=>{
    document.title = to.meta.title || '教师管理系统'
})

export default router